		TITLE	SEGMRELO - Copyright (c) SLR Systems 1994

		INCLUDE	MACROS
		INCLUDE	SEGMENTS
		INCLUDE	RELOCSS
		INCLUDE	SEGMSYMS

if	fg_segm

		PUBLIC	OS2_RELOC_FLUSH


		.DATA

		EXTERNDEF	TEMP_RECORD:BYTE

		EXTERNDEF	RELOC_COUNT:DWORD,MOVABLE_MASK:DWORD,LAST_SEG_OS2_NUMBER:DWORD,RELOC_BITS_LOG:DWORD
		EXTERNDEF	LIDATA_RELOCS_START:DWORD,RELOC_HIGH_WATER:DWORD,RELOC_BITS:DWORD,LIDATA_RELOCS_NEXT:DWORD
		EXTERNDEF	OLD_HIGH_WATER:DWORD,HIGH_WATER:DWORD,NEW_REPT_ADDR:DWORD,FINAL_HIGH_WATER:DWORD
		EXTERNDEF	EXEPACK_SEGMENT_START:DWORD,FIRST_RELOC_GINDEX:DWORD,LAST_RELOC_GINDEX:DWORD

		EXTERNDEF	RELOC_STUFF:ALLOCS_STRUCT,RELOC_GARRAY:STD_PTR_S,SEGMENT_TABLE:SEGTBL_STRUCT


		.CODE	PASS2_TEXT

		EXTERNDEF	SEARCH_ENTRY:PROC,ERR_RET:PROC,_release_minidata:proc,RELEASE_BLOCK:PROC
		EXTERNDEF	MOVE_EAX_TO_FINAL_HIGH_WATER:PROC,RELEASE_SEGMENT:PROC,RELEASE_GARRAY:PROC

		EXTERNDEF	RC_64K_ERR:ABS


OS2_RELOC_FLUSH PROC
		;
		;IF THERE IS ANY RELOCATION INFORMATION, SET BIT IN SEGMENT
		;TABLE AND THEN WRITE THE STUFF OUT.. RELEASE ALL INFO
		;
		PUSHM	EDI,ESI,EBX
		MOV	EAX,RELOC_COUNT		;# OF RELOCS THIS SEGMENT

		TEST	EAX,EAX
		JZ	L91$

		MOV	EDI,OFF TEMP_RECORD+2
		MOV	DPTR TEMP_RECORD,EAX

		CMP	EAX,8
		JBE	L0$

		CALL	OS2_RELOC_F1

L0$:
		MOV	ESI,FIRST_RELOC_GINDEX
		JMP	L22$

L7$:
		CALL	OS2_RELOC_F1
		JMP	L21$

L1$:
		CONVERT	ESI,ESI,RELOC_GARRAY
		ASSUME	ESI:PTR RELOC_STRUCT

		MOV	EAX,[ESI]._RL_FLAGS
		MOV	EDX,[ESI]._RL_SEG_OFFSET

		MOV	ECX,[ESI]._RL_FLAGS
		AND	AH,0F7H			;CLEAR IGNORE-MOVABLE BIT

		MOV	[EDI],EAX
		MOV	EBX,[ESI]._RL_NEXT_RELOC_GINDEX

		MOV	[EDI+2],DL
		PUSH	EBX

		MOV	[EDI+3],DH
		ADD	EDI,4

		AND	AH,3			;IMPORT?
		JZ	L3$

		CMP	AH,3			;OS_FIXUP
		MOV	EAX,[ESI]._RL_IMP_MODULE

		MOV	ECX,[ESI]._RL_IMP_OFFSET
		JZ	L15$

		CONVERT	EAX,EAX,IMPMOD_GARRAY
		ASSUME	EAX:PTR IMPMOD_STRUCT
		MOV	EAX,[EAX]._IMPM_NUMBER

L15$:
		MOV	[EDI+2],CX

		MOV	[EDI],AX
		ADD	EDI,4
L2$:
		CMP	EDI,OFF TEMP_RECORD+1K		;FLUSH THIS STUFF 1K BLOCKS
		JAE	L7$
L21$:
		POP	ESI
L22$:
		TEST	ESI,ESI
		JNZ	L1$

		JMP	L8$

L3$:
		;
		;INTERNAL REFERENCE...
		;
		AND	CH,8				;IGNORE MOVABILITY?
		JNZ	L36$

		MOV	EAX,[ESI]._RL_OS2_NUMBER	;SEE IF THIS SEGMENT IS

		CONV_EAX_SEGTBL_ECX		;MOVABLE

		MOV	EAX,MOVABLE_MASK
		MOV	EBX,[ECX]._SEGTBL_FLAGS

		AND	EAX,EBX
		JNZ	L4$
L35$:

L36$:
		;
		;PLAIN OLD INTERNAL
		;
		MOV	EAX,[ESI]._RL_OS2_NUMBER
		MOV	ECX,[ESI]._RL_TARG_OFFSET

		MOV	[EDI],EAX
		ADD	EDI,4

		MOV	[EDI-2],CX
		JMP	L2$

L4$:
		;
		;PROBABLE ENTRY-POINT REFERENCE
		;
		AND	EAX,MASK SR_MOVABLE+MASK SR_DISCARD
		JNZ	L41$

		AND	EBX,MASK SR_CONF+1
		JNZ	L35$
L41$:
		;
		;ENTRY-POINT REFERENCE...
		;
		MOV	DPTR [EDI],00FFH
		;
		;NOW LOOK UP ENTRY POINT IN ENTRY TABLE TO GET ORD NUMBER
		;
		MOV	EAX,[ESI]._RL_TARG_OFFSET

		MOV	EDX,[ESI]._RL_OS2_NUMBER
		CALL	SEARCH_ENTRY

		MOV	[EDI+2],AX
		LEA	EDI,[EDI+4]

		JNC	L2$

		MOV	AL,0
		CALL	ERR_RET
		JMP	L2$

L8$:
		CALL	OS2_RELOC_F1		;FLUSH TEMP_RECORD
		;
		;RELEASE RELOC SPACE
		;
		MOV	EAX,OFF RELOC_STUFF
		push	EAX
		call	_release_minidata
		add	ESP,4

		MOV	EAX,OFF RELOC_GARRAY
		CALL	RELEASE_GARRAY

		XOR	EAX,EAX

		MOV	RELOC_STUFF.ALLO_HASH_TABLE_PTR,EAX
		MOV	RELOC_COUNT,EAX

		MOV	RELOC_HIGH_WATER,EAX
		MOV	LIDATA_RELOCS_NEXT,EAX

		MOV	FIRST_RELOC_GINDEX,EAX
		MOV	LAST_RELOC_GINDEX,EAX

		XCHG	RELOC_BITS,EAX

		CALL	RELEASE_BLOCK

		XOR	EAX,EAX

		XCHG	LIDATA_RELOCS_START,EAX

if	page_size EQ 8K

		OR	EAX,EAX
		JZ	L85$

		CALL	RELEASE_BLOCK
L85$:
endif

		MOV	EAX,LAST_SEG_OS2_NUMBER

		CONV_EAX_SEGTBL_ECX

		GETT	AL,RC_PRELOADS

		OR	[ECX]._SEGTBL_FLAGS,MASK SR_RELOC


		OR	AL,AL
		JZ	L91$

		MOV	EAX,FINAL_HIGH_WATER		;MAKE SURE PRELOADED SEGMENT + RELOCS
		MOV	ECX,EXEPACK_SEGMENT_START	;IS NOT >= 64K

		SUB	EAX,ECX

		CMP	EAX,64K
		JAE	L99$
L91$:
		XOR	EAX,EAX
		POPM	EBX,ESI,EDI

		MOV	OLD_HIGH_WATER,EAX
		MOV	HIGH_WATER,EAX

		MOV	NEW_REPT_ADDR,EAX

		RET

L99$:
		MOV	AX,RC_64K_ERR
		CALL	ERR_RET

		JMP	L91$

OS2_RELOC_FLUSH ENDP


OS2_RELOC_F1	PROC	NEAR
		;
		;
		;
		MOV	ECX,EDI
		MOV	EAX,OFF TEMP_RECORD

		SUB	ECX,EAX
		JZ	L9$

		MOV	EDI,EAX
		JMP	MOVE_EAX_TO_FINAL_HIGH_WATER

L9$:
		RET

OS2_RELOC_F1	ENDP

endif

		END

